﻿using EFModel;
using IServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xm.Controllers;

namespace Serives
{
    public class Tesalary_standard : Isalary_standard
    {
        private readonly MyDBContext he;

        public Tesalary_standard(MyDBContext he)
        {
            this.he = he;
        }
        //薪酬基本信息查询
        public Task<string> Ate()
        {
            return Task.Run(() =>
            {
                return JsonConvert.SerializeObject(he.salary_standard);
            });
        }
        //查询公共属性薪酬数据
        public Task<string> Auto(string name)
        {
            return Task.Run(() =>
            {
                return JsonConvert.SerializeObject(he.config_public_char.Where(e => e.attribute_name == name).ToList());
            });
        }
        //薪酬基本查询
        public Task<salary_standard> Chui(int id)
        {
            return Task.Run(() =>
            {
                return he.salary_standard.Find(id);
            });
        }
        //薪酬详细数据表查询
        public Task<string> ChuiTo(string id)
        {
            return Task.Run(() =>
            {
                return JsonConvert.SerializeObject(he.salary_standard_details.Where(e => e.standard_id == id));
            });
        }

        public Task<string> Fall()
        {
            return Task.Run(() =>
            {
                return JsonConvert.SerializeObject(he.salary_standard);
            });
        }

        public Task<string> FallT(string id)
        {
            return Task.Run(() =>
            {
                return JsonConvert.SerializeObject(he.salary_standard.Where(e=> e.standard_id == id));
            });
        }
        //薪酬标准添加
        public Task<int> Insert(salary_standard salary_)
        {
            var io = he.Database.BeginTransaction();
            return Task.Run(() =>
            {
                try
                {
                    he.salary_standard.Add(salary_);
                    int num = he.SaveChangesAsync().Result;
                    if (num > 0 )
                    {
                        //事物提交
                        io.Commit();
                    }
                    return num;
                }
                catch (Exception)
                {
                    io.Rollback();
                    throw;
                }           
            });
        }
        //薪酬标准详细添加
        public Task<int> Inserte(salary_standard_details salary)
        {
            return Task.Run(() =>
            {
                he.salary_standard_details.Add(salary);
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬查询
        public Task<ResultInfo> Quer(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_standard.Where(e=> e.check_status==0);
                var list = he.salary_standard
                             .Where(e => e.check_status == 0)
                    .OrderBy(q => q.ssd_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬为通过查询
        public Task<ResultInfo> Querse(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_standard.Where(e => e.check_status == 0);
                var list = he.salary_standard
                                    .Where(e => e.check_status == 0)
                    .OrderBy(q => q.ssd_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬通过查询
        public Task<ResultInfo> QuerseTo(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_standard.Where(e => e.check_status == 1);
                var list = he.salary_standard
                                    .Where(e => e.check_status == 1)
                    .OrderBy(q => q.ssd_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬为通过模糊查询
        public Task<ResultInfo> QuerSeo(salary_standard salary_, int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_standard.Where(e => e.check_status == 0
                && e.ssd_id == salary_.ssd_id
                &&e.standard_name.Contains(salary_.standard_name)
                && e.regist_time.Date >= salary_.regist_time
                && e.regist_time.Date <= salary_.check_time);
                var list = he.salary_standard
                .Where(e => e.check_status == 0
                && e.ssd_id == salary_.ssd_id
                && e.standard_name.Contains(salary_.standard_name)
                && e.regist_time.Date >= salary_.regist_time
                && e.regist_time.Date <= salary_.check_time)
                .OrderBy(q => q.ssd_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬基本复核过模糊查询
        public Task<ResultInfo> QuerSeoTo(salary_standard salary_, int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.salary_standard.Where(e => e.check_status == 1
                && e.ssd_id == salary_.ssd_id
                && e.standard_name.Contains(salary_.standard_name)
                && e.regist_time.Date >= salary_.regist_time
                && e.regist_time.Date <= salary_.check_time);
                var list = he.salary_standard
                .Where(e => e.check_status == 1
                && e.ssd_id == salary_.ssd_id
                && e.standard_name.Contains(salary_.standard_name)
                && e.regist_time.Date >= salary_.regist_time
                && e.regist_time.Date <= salary_.check_time)
                .OrderBy(q => q.ssd_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //薪酬复核通过修改
        public Task<int> Updateou(salary_standard standard)
        {
            return Task.Run(() =>
            {
                salary_standard salary=he.salary_standard.Find(standard.ssd_id);
                salary.checker = standard.checker;
                salary.check_time = standard.check_time;
                salary.check_comment = standard.check_comment;
                salary.check_status = 1;
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬基本修改
        public Task<int> Upte(salary_standard salary_)
        {
            return Task.Run(() =>
            {
                salary_standard salary = he.salary_standard.Find(salary_.ssd_id);
                salary.standard_name = salary_.standard_name;
                salary.changer = salary_.changer;
                salary.change_time = salary_.change_time;
                salary.salary_sum = salary_.salary_sum;
                salary.check_status = 0;
                salary.remark = salary_.remark;
                salary.change_status += 1;
                int num = he.SaveChanges();
                return num;
            });
        }
        //薪酬详细修改
        public Task<int> UpteTo(salary_standard_details salary_)
        {
            return Task.Run(() =>
            {
                salary_standard_details salary = he.salary_standard_details.Find(salary_.sdt_id);
                salary.standard_name = salary_.standard_name;
                salary.salary = salary_.salary;
                int num = he.SaveChanges();
                return num;
            });
        }
    }
}
